package com.qph.app.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qph.app.system.service.IRoleService;
import com.qph.base.exception.BusinessException;
import com.qph.base.security.entity.Permission;
import com.qph.base.security.entity.Role;
import com.qph.utils.base.StringUtil;

/**
 * 角色管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/role")
public class RoleController {

	@Resource(name="roleService")
	private IRoleService roleService;
	
	/**
	 * 打酱油~跳转角色列表页面
	 * @return
	 */
	@RequestMapping(value="/roleList")
	@RequiresPermissions("Role:query")
	public String roleList(){
		return "/system/role/roleList";
	}
	
	/**
	 * ajax查询角色列表--无分页
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getRoleList")
	@RequiresPermissions("Role:query")
	public List<Role> getUserList(Role role){
		return roleService.getModelList(role);
	}
	
	/**
	 * 打酱油~跳转到增加角色页面
	 * @return
	 */
	@RequestMapping(value="/addRole")
	@RequiresPermissions("Role:add")
	public String addRole(){
		return "/system/role/addRole";
	}
	
	
	/**
	 * 修改角色
	 * @return
	 */
	@RequestMapping(value="/editRole")
	@RequiresPermissions("Role:update")
	public String editRole(HttpServletRequest request,String id){
		Role role = roleService.getModel(id);
		request.setAttribute("role", role);
		return "/system/role/editRole";
	}
	
	
	/**
	 * 保存角色
	 * @param role
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
//	@RequiresPermissions("Role:add")
	@RequestMapping(value="/saveRole")
	public JSONObject saveRole(Role role) throws BusinessException{
		
		boolean result = false;
		
		JSONObject json = new JSONObject();
		
		if (StringUtil.isEmpty(role.getId())){
			result = roleService.insert(role);
		} else {
			result = roleService.update(role);
		}
		
		json.put("result", result);
		return json;
	}
	
	/**
	 * 删除角色
	 * @param role
	 * @return
	 * @throws BusinessException 
	 */
	@ResponseBody
	@RequestMapping(value="/delRole")
	@RequiresPermissions("Role:delete")
	public JSONObject delRole(Role role) throws BusinessException{
		int delCount = roleService.delete(role.getId());
		JSONObject json = new JSONObject();
		json.put("result", delCount > 0);
		return json;
	}
	
	
	/**
	 * 分配用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/fpyh")
	@RequiresPermissions("Role:grantUser")
	public String fpyh(HttpServletRequest request, String id){
		
		Role role = roleService.getModel(id);
		request.setAttribute("role", role);
		return "/system/role/fpyh";
	}
	
	
	/**
	 * 角色分配用户
	 * @param ids
	 * @param roleId
	 * @return
	 * @throws BusinessException
	 */
	@ResponseBody
	@RequestMapping(value="/saveRoleUsers")
	@RequiresPermissions("Role:grantUser")
	public boolean saveRoleUsers(String ids,String roleId)throws BusinessException{
		return roleService.saveRoleUsers(ids, roleId);
	}
	
	
	/**
	 * 删除角色下用户
	 * @param ids
	 * @param roleId
	 * @return
	 * @throws BusinessException
	 */
	@ResponseBody
	@RequestMapping(value="/delRoleUsers")
	@RequiresPermissions("Role:grantUser")
	public boolean delRoleUsers(String ids,String roleId)throws BusinessException{
		return roleService.delRoleUsers(ids, roleId);
	}
	
	/**
	 * 角色授权
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/grantPerm")
	@RequiresPermissions("Role:grantPerm")
	public String grantPerm(HttpServletRequest request, String id){
		
		Role role = roleService.getModel(id);
		request.setAttribute("role", role);
		return "/system/role/grantPerm";
	}


	/**
	 * 保存角色权限
	 * @param pids
	 * @param roleId
	 * @return
	 * @throws BusinessException
	 */
	@ResponseBody
	@RequestMapping(value="/saveRolePerm")
	@RequiresPermissions("Role:grantPerm")
	public boolean saveRolePerm(String ids,String roleId)throws BusinessException{
		
		return roleService.saveRolePerm(ids, roleId);
	}
	
	
	/**
	 * 按角色Id查询拥有的权限资源 
	 * @param roleId
	 * @return
	 * @throws BusinessException
	 */
	@ResponseBody
	@RequestMapping(value="/getPermListByRole")
	public List<Permission> getPermListByRole(String roleId)throws BusinessException{
		
		return roleService.getPermListByRole(roleId);
	}

}
